package org.longsheng.GroovyLearn.java.aop;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class AopTest {
    private static Word word;

    public static void main(String[] args) {
        String content = "京东的刘强东在肯德基吃啃得起";

        testNormal(content);

        System.out.println("----------------");

        testStaticProxy(content);

        System.out.println("----------------");

        testDynamicProxy(content);
    }

    private static void testNormal(String content) {
        System.out.println("正常执行测试start...");

        init(ProxyType.NORMAL);
        List<String> keyWordList = word.findKeyword(content);

        try {
            log.info("日志：{} - {}", content, keyWordList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("正常执行测试end...");
    }

    private static void testStaticProxy(String content) {
        System.out.println("静态代理执行测试start...");
        init(ProxyType.STATIC_PROXY);
        List<String> keyWordList = word.findKeyword(content);
        System.out.println("静态代理执行测试end...");
    }

    private static void testDynamicProxy(String content) {
        System.out.println("动态代理执行测试start...");

        init(ProxyType.DYNAMIC_PROXY);
        List<String> keyWordList = word.findKeyword(content);

        System.out.println("执行结果：" + keyWordList);
        System.out.println("动态代理执行测试end...");
    }

    private static void init(ProxyType proxyType) {
        switch (proxyType) {
            case STATIC_PROXY:
                word = createStaticProxy();
                break;
            case DYNAMIC_PROXY:
                word = createDynamicProxy();
                break;
            default:
                word = createWord();
        }
    }

    /**
     * 创建实例
     * @return
     */
    private static Word createWord() {
        Word wordTmp = new WordImpl();
        wordTmp.addWord("啃得起");
        wordTmp.addWord("京东");
        wordTmp.addWord("拼多多");
        wordTmp.addWord("马云");

        return wordTmp;
    }

    /**
     * 创建静态代理类
     * @return
     */
    private static Word createStaticProxy() {
        Word word = createWord();
        WordStaticProxy proxy = new WordStaticProxy();
        proxy.setWord(word);
        return proxy;
    }

    /**
     * 创建动态代理类
     * @return
     */
    private static Word createDynamicProxy() {
        Word word = createWord();

        return DynamicProxyUtil.getProxy(Word.class, word);
    }

    public enum ProxyType {
        NORMAL,
        STATIC_PROXY,
        DYNAMIC_PROXY
    }
}
